package com.accloud.cloudservice;

import com.accloud.common.ACConfiguration;
import com.accloud.service.ACException;
import com.accloud.service.ACMsg;
import com.accloud.service.ACOTACheckInfo;
import com.accloud.service.ACOTAFileInfo;
import com.accloud.service.ACOTAMgr;
import com.accloud.service.ACOTAUpgradeInfo;
import com.accloud.service.ACObject;
import cz.msebera.android.httpclient.cookie.ClientCookie;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONException;

public class ACOTAManager extends ACBaseManager implements ACOTAMgr {
    public ACOTAManager() {
        super(ACConfiguration.OTA_SERVICE_NAME, 1, true);
    }

    @Override // com.accloud.service.ACOTAMgr
    public void checkUpdate(String subDomain, final ACOTACheckInfo checkInfo, final PayloadCallback<ACOTAUpgradeInfo> callback) {
        BaseCallback innerCallback = new BaseCallback() {
            /* class com.accloud.cloudservice.ACOTAManager.AnonymousClass1 */

            @Override // com.accloud.cloudservice.BaseCallback
            public void error(ACException e) {
                AC.sEventManager.track(new OTAEvent("check", checkInfo.getPhysicalDeviceId(), checkInfo.getDeviceId(), checkInfo.getVersion(), null));
                callback.error(e);
            }
        };
        if (subDomain.length() <= 0) {
            innerCallback.error(new ACException(ACException.INVALID_PARAMETERS, "invalid parameters"));
        } else if (!AC.accountMgr().isLogin()) {
            innerCallback.error(new ACException(ACException.NO_LOGIN, "no login"));
        } else {
            ACMsg req = ACMsg.getACMsg(subDomain);
            req.setName("checkUpdate");
            if (checkInfo.getDeviceId() != 0) {
                req.put("deviceId", Long.valueOf(checkInfo.getDeviceId()));
            }
            if (checkInfo.getPhysicalDeviceId() != null) {
                req.put("physicalDeviceId", checkInfo.getPhysicalDeviceId());
            }
            if (checkInfo.getVersion() != null) {
                req.put(ClientCookie.VERSION_ATTR, checkInfo.getVersion());
            }
            req.put("otaType", Integer.valueOf(checkInfo.getOtaType()));
            req.put("channel", checkInfo.getChannel());
            req.put("batch", checkInfo.getBatch());
            sendReq(req, innerCallback, new ACMsgListener() {
                /* class com.accloud.cloudservice.ACOTAManager.AnonymousClass2 */

                @Override // com.accloud.cloudservice.ACMsgListener
                public void finish(ACMsg resp) {
                    if (resp.getInt("update") == 0) {
                        AC.sEventManager.track(new OTAEvent("check", checkInfo.getPhysicalDeviceId(), checkInfo.getDeviceId(), checkInfo.getVersion(), null));
                        callback.success(new ACOTAUpgradeInfo(false, "you have not OTA update"));
                        return;
                    }
                    String currentVersion = resp.getString("currentVersion");
                    String targetVersion = resp.getString("targetVersion");
                    int status = resp.getInt("status");
                    int otaMode = resp.getInt("otaMode");
                    String upgradeLog = resp.getString("upgradeLog");
                    List<ACObject> objects = (List) resp.get("files");
                    List<ACOTAFileInfo> files = new ArrayList<>();
                    if (objects != null) {
                        for (ACObject object : objects) {
                            files.add(new ACOTAFileInfo(object.getString("name"), object.getInt("type"), object.getInt("checksum"), object.getString("downloadUrl")));
                        }
                    }
                    AC.sEventManager.track(new OTAEvent("check", checkInfo.getPhysicalDeviceId(), checkInfo.getDeviceId(), currentVersion, targetVersion));
                    callback.success(new ACOTAUpgradeInfo(currentVersion, targetVersion, otaMode, upgradeLog, status, files));
                }
            });
        }
    }

    @Override // com.accloud.service.ACOTAMgr
    public void confirmUpdate(String subDomain, long deviceId, String targetVersion, int otaType, VoidCallback callback) {
        VoidEventCallback callbackWrapper = new VoidEventCallback(new OTAEvent("confirm", null, deviceId, null, targetVersion), callback);
        if (targetVersion.length() <= 0 || subDomain.length() <= 0 || deviceId <= 0) {
            callbackWrapper.error(new ACException(ACException.INVALID_PARAMETERS, "invalid parameters"));
        } else if (!AC.accountMgr().isLogin()) {
            callbackWrapper.error(new ACException(ACException.NO_LOGIN, "no login"));
        } else {
            ACMsg req = ACMsg.getACMsg(subDomain);
            req.setName("confirmUpdate");
            req.put("deviceId", Long.valueOf(deviceId));
            req.put("targetVersion", targetVersion);
            req.put("otaType", Integer.valueOf(otaType));
            sendReq(req, callbackWrapper);
        }
    }

    @Override // com.accloud.service.ACOTAMgr
    public void otaMediaDone(String subDomain, String physicalDeviceId, String currentVersion, String targetVersion, VoidCallback callback) {
        if (targetVersion.length() <= 0 || subDomain.length() <= 0 || physicalDeviceId.length() <= 0) {
            callback.error(new ACException(ACException.INVALID_PARAMETERS, "invalid parameters"));
        } else if (!AC.accountMgr().isLogin()) {
            callback.error(new ACException(ACException.NO_LOGIN, "no login"));
        } else {
            ACMsg req = ACMsg.getACMsg(subDomain);
            req.setName("otaMediaDone");
            req.put("physicalDeviceId", physicalDeviceId);
            req.put("currentVersion", currentVersion);
            req.put("otaType", 1);
            req.put("targetVersion", targetVersion);
            sendReq(req, callback);
        }
    }

    private class OTAEvent extends ACEvent {
        private static final String CHECK = "check";
        private static final String CONFIRM = "confirm";

        private OTAEvent(String eventName, String physicalDeviceId, long deviceId, String originVersion, String destVersion) {
            super("apm_app_device_ota", eventName, null);
            try {
                this.physicalDeviceId = physicalDeviceId;
                if (deviceId > 0) {
                    this.deviceId = Long.valueOf(deviceId);
                }
                if (originVersion != null) {
                    this.data.put("origin_version", originVersion);
                }
                if (destVersion != null) {
                    this.data.put("dest_version", destVersion);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}
